package com.euond.permission.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.euond.common.core.dto.ResultDto;
import com.euond.common.core.dto.SerachMapDto;
import com.euond.common.web.controller.BaseCRUDController;
import com.euond.common.web.feign.uc.UserDto;
import com.euond.common.web.feign.uc.UserFeignService;
import com.euond.permission.entities.UserRole;
import com.euond.permission.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by user on 2017/7/24.
 */
@RestController
@RequestMapping("permission/front/userroles")
public class UserRoleController extends BaseCRUDController<UserRoleService,UserRole> {

    @Autowired
    UserFeignService userFeignService;

    /**
     * 根据用户查询已分配的角色(分页)
     */
    @GetMapping(value = "user/role")
    public ResultDto findUserRole(@RequestParam Map<String,Object> searchParams) {
        return baseService.findUserAvailableRoles(searchParams);
    }

    /**
     * 根据角色查询未分配的用户（分页）
     */
    @GetMapping(value = "role/no/user")
    public ResultDto findRoleNotAvailableUsers(@RequestParam Map<String,Object> searchParams) {
        ResultDto resultDto=null;
        //前端需要传roleId,companyId
        UserRole userRole=new UserRole();
        SerachMapDto serachMapDto=new SerachMapDto(searchParams,userRole);
        List<UserRole> userRoleList = baseService.findUserRoleByRoleId(userRole.getRoleId());
        List<Long> userIds=new ArrayList<Long>();
        //userIds不能为空传过去，需默认一个不存在的值
        userIds.add(Long.valueOf("-1"));
        for(UserRole userRole1:userRoleList){
            //ID去重
            if(!userIds.contains(userRole1.getUserId())){
                userIds.add(userRole1.getUserId());
            }
        }
        Page<UserDto> userDtoPage=userFeignService.findRoleNotAvailableUsers(userIds,userRole.getCompanyId(),serachMapDto.getPageCurrent(),serachMapDto.getPageSize());
        if(userDtoPage.getTotal()>0){
            resultDto=new ResultDto("0","seccess",userDtoPage.getRecords(),userDtoPage.getTotal());
        }else{
            resultDto=new ResultDto("9009","没有查到任何数据！");
        }
        return resultDto;
    }
    /**
     * 根据角色查询已分配的用户（分页）
     */
    @GetMapping(value = "role/user")
    public ResultDto findRoleAvailableUsers(@RequestParam Map<String,Object> searchParams) {
        ResultDto resultDto=null;
        Page<UserRole> userRolePage= baseService.selectListPageByMap(searchParams);
        if(userRolePage.getTotal()>0){
            List<UserRole> userRoleList=userRolePage.getRecords();
            List<Long> userIds=new ArrayList<Long>();
            for(UserRole userRole:userRoleList){
                //ID去重
                if(!userIds.contains(userRole.getUserId())){
                    userIds.add(userRole.getUserId());
                }
            }
            List<UserDto> userDtoList =userFeignService.findUsersByIds(userIds);
            if(userDtoList.size()>0){
                resultDto=new ResultDto("0","seccess",userDtoList,userRolePage.getTotal());
            }else{
                resultDto=new ResultDto("9009","没有查到任何数据！");
            }
        }
        return resultDto;
    }

    /**
     * 根据用户查询未分配的角色（分页）
     */
    @GetMapping(value = "user/no/role")
    public ResultDto findUserNotAvailableRoles(@RequestParam Map<String,Object> searchParams) {
        return baseService.findUserNotAvailableRoles(searchParams);
    }

    /**
     * 按条件查询实体列表以分页返回(覆盖基类)
     */
    @GetMapping(value = "page")
    public ResultDto listPage(@RequestParam(required = false) Map<String, Object> map) {
        ResultDto resultDto = null;
        Page<UserRole> userRolePage = baseService.findUserRoles(map);

        if (userRolePage.getTotal() > 0) {
            List<UserRole> userRoleList = userRolePage.getRecords();
            List<Long> userIds = new ArrayList<Long>();
            for (UserRole userRole : userRoleList) {
                //ID去重
                if (!userIds.contains(userRole.getUserId())) {
                    userIds.add(userRole.getUserId());
                }
            }
            List<UserDto> userDtoList = userFeignService.findUsersByIds(userIds);
            if (userDtoList.size() > 0) {
                for (int i = 0; i < userRoleList.size(); i++) {
                    //公司
                    for (UserDto userDto : userDtoList) {
                        if (userRoleList.get(i).getUserId().equals(userDto.getId())) {
                            userRoleList.get(i).setUsername(userDto.getUsername());
                            userRoleList.get(i).setLoginName(userDto.getLoginName());
                        }
                    }
                }
                resultDto = new ResultDto("0", "seccess", userRoleList, userRolePage.getTotal());
            } else {
                resultDto = new ResultDto("9009", "没有查到任何数据！");
            }
        }
        return resultDto;
    }
}
